<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.Project"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="db.ProjectDAO"%>
<%@page import="db.SubstoreDAO" %>
<%@page import="db.PurreqDAO" %>
<%@page import="db.Purreq" %>
<%@page import="db.OutreqDAO"%>
<%@page import="db.RetreqDAO"%>
<%@page import="db.BorreqDAO"%>
<%@page import="db.ToreqDAO"%>
<%@page import="db.BackreqDAO"%>
<%@page import="java.io.File"%>
<%@page import="java.io.FileOutputStream" %>  
<%@page import="java.io.OutputStream" %>  
<%@page import="jxl.Workbook" %>  
<%@page import="jxl.format.Alignment" %> 
<%@page import="jxl.format.Border" %> 
<%@page import="jxl.format.BorderLineStyle" %>  
<%@page import="jxl.format.Colour" %>  
<%@page import="jxl.format.VerticalAlignment" %>  
<%@page import="jxl.write.Label" %>  
<%@page import="jxl.write.WritableCellFormat" %>  
<%@page import="jxl.write.WritableFont" %>  
<%@page import="jxl.write.WritableSheet" %>
<%@page import="jxl.write.WritableWorkbook" %> 
<%@page import="java.text.SimpleDateFormat"%>
<%@ include file="/jsp/common/utility/volti.jsp" %>
<%@ include file="/jsp/common/utility/logined.jsp" %>
<%
	request.setCharacterEncoding("utf-8");
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	PurreqDAO purreqDAO = (PurreqDAO)ctx.getBean("PurreqDAO");
	OutreqDAO outreqDAO = (OutreqDAO)ctx.getBean("OutreqDAO");
	RetreqDAO retreqDAO = (RetreqDAO)ctx.getBean("RetreqDAO");
	BorreqDAO borreqDAO = (BorreqDAO)ctx.getBean("BorreqDAO");
	ToreqDAO toreqDAO = (ToreqDAO)ctx.getBean("ToreqDAO");
	BackreqDAO backreqDAO = (BackreqDAO)ctx.getBean("BackreqDAO");
	
	int pn=0;
	
	//每页多少条
	String execl="";
	int pageSize = 0;
	int uid = (Integer)session.getAttribute("id");
	String type = request.getParameter("type");
	String beginDate = request.getParameter("beginDate");
	String endDate = request.getParameter("endDate");
	String searchType = request.getParameter("searchType");
	String searchWord = request.getParameter("searchWord");
	
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	List ob = new ArrayList();
	Map map = null;
	
	ob.add(new Object[]{"eq","reqUserId",uid});
	
	if(beginDate != null &&!"".equals(beginDate)){
		Date date = sdf.parse(beginDate);
		ob.add(new Object[]{"ge","reqDate",date});
	}
	
	if(endDate != null &&!"".equals(endDate)){
		Date date = sdf.parse(endDate);
		ob.add(new Object[]{"le","reqDate",date});
	}

	if("1".equals(type)){
		if("1".equals(searchType)){
			ob.add(new Object[]{"eq","reqCode",searchWord});
		}else if("2".equals(searchType)){
			ob.add(new Object[]{"eq","p.code",searchWord});
		}else if("3".equals(searchType)){
			ob.add(new Object[]{"eq","p.name",searchWord});
		}
		
		map = purreqDAO.getPurreq(pn,pageSize,
				new String[][]{new String[]{"desc","date"}},
				ob,
				new String[][]{new String[]{"","id"},new String[]{"","reqCode"},new String[]{"","reqDate"},new String[]{"","p.code"},new String[]{"","p.name"},new String[]{"","state"}},
				new String[]{"project"}
				);
		int pageTotal = (Integer)map.get("pageTotal");
		List pageList = getPage(pageTotal,pn);
		request.setAttribute("map",map);
		execl = getExecl(request,map);
		request.setAttribute("pages",pageList);
	}else if("2".equals(type)){
		if("1".equals(searchType)){
			ob.add(new Object[]{"eq","outCode",searchWord});
		}else if("2".equals(searchType)){
			ob.add(new Object[]{"eq","p.code",searchWord});
		}else if("3".equals(searchType)){
			ob.add(new Object[]{"eq","p.name",searchWord});
		}
		
		map = outreqDAO.getOutreq(pn,pageSize,
				new String[][]{new String[]{"desc","id"}},
				ob,
				new String[][]{new String[]{"","id"},new String[]{"","outCode"},new String[]{"","reqDate"},new String[]{"","p.code"},new String[]{"","p.name"},new String[]{"","phase"}},
				new String[]{"","project"}
				);
		int pageTotal = (Integer)map.get("pageTotal");
		List pageList = getPage(pageTotal,pn);
		request.setAttribute("map",map);
		request.setAttribute("pages",pageList);
		execl = getExecl(request,map);
	}else if("3".equals(type)){
		if("1".equals(searchType)){
			ob.add(new Object[]{"eq","reqCode",searchWord});
		}else if("2".equals(searchType)){
			ob.add(new Object[]{"eq","op.code",searchWord});
		}else if("3".equals(searchType)){
			ob.add(new Object[]{"eq","op.name",searchWord});
		}
		
		map = retreqDAO.getRetreq(pn,pageSize,
				new String[][]{new String[]{"desc","reqDate"}},
				ob,
				new String[][]{new String[]{"","id"},new String[]{"","reqCode"},new String[]{"","reqDate"},new String[]{"","op.code"},new String[]{"","op.name"},new String[]{"","phase"}},
				new String[]{"outreq","project"}
				);
		int pageTotal = (Integer)map.get("pageTotal");
		List pageList = getPage(pageTotal,pn);
		request.setAttribute("map",map);
		request.setAttribute("pages",pageList);
		execl = getExecl(request,map);
	}else if("4".equals(type)){
		if("1".equals(searchType)){
			ob.add(new Object[]{"eq","borCode",searchWord});
		}else if("2".equals(searchType)){
			ob.add(new Object[]{"eq","p.code",searchWord});
		}else if("3".equals(searchType)){
			ob.add(new Object[]{"eq","p.name",searchWord});
		}
		
		map = borreqDAO.getBorreq(pn,pageSize,
				new String[][]{new String[]{"desc","id"}},
				ob,
				new String[][]{new String[]{"","id"},new String[]{"","borCode"},new String[]{"","reqDate"},new String[]{"","p.code"},new String[]{"","p.name"},new String[]{"","phase"}},
				new String[]{"","project"}
				);
		int pageTotal = (Integer)map.get("pageTotal");
		List pageList = getPage(pageTotal,pn);
		request.setAttribute("map",map);
		request.setAttribute("pages",pageList);
		execl = getExecl(request,map);
	}else if("5".equals(type)){
		if("1".equals(searchType)){
			ob.add(new Object[]{"eq","toCode",searchWord});
		}else if("2".equals(searchType)){
			ob.add(new Object[]{"eq","op.code",searchWord});
		}else if("3".equals(searchType)){
			ob.add(new Object[]{"eq","op.name",searchWord});
		}
		
		map = toreqDAO.getTomatreq(pn,pageSize,
				new String[][]{new String[]{"desc","id"}},
				ob,
				new String[][]{new String[]{"","id"},new String[]{"","toCode"},new String[]{"","reqDate"},new String[]{"","op.code"},new String[]{"","op.name"},new String[]{"","phase"}},
				new String[]{"borreq","project"}
				);
		int pageTotal = (Integer)map.get("pageTotal");
		List pageList = getPage(pageTotal,pn);
		request.setAttribute("map",map);
		request.setAttribute("pages",pageList);
		execl = getExecl(request,map);
	}else if("6".equals(type)){
		if("1".equals(searchType)){
			ob.add(new Object[]{"eq","backCode",searchWord});
		}else if("2".equals(searchType)){
			ob.add(new Object[]{"eq","op.code",searchWord});
		}else if("3".equals(searchType)){
			ob.add(new Object[]{"eq","op.name",searchWord});
		}
		
		map = backreqDAO.getBackreq(pn,pageSize,
				new String[][]{new String[]{"desc","id"}},
				ob,
				new String[][]{new String[]{"","id"},new String[]{"","backCode"},new String[]{"","reqDate"},new String[]{"","op.code"},new String[]{"","op.name"},new String[]{"","phase"}},
				new String[]{"borreq","project"}
				);
		int pageTotal = (Integer)map.get("pageTotal");
		List pageList = getPage(pageTotal,pn);
		request.setAttribute("map",map);
		request.setAttribute("pages",pageList);
		execl = getExecl(request,map);
	}
	request.setAttribute("searchType",searchType);
	request.setAttribute("searchType",searchType);
	request.setAttribute("searchWord",searchWord);
	request.setAttribute("type",type);
	request.setAttribute("xlsName",execl);
	pageContext.forward("/vm/search/seachRequest.vm");
%>
<%!
private String getExecl(HttpServletRequest request,Map map) {
		///导出execl
		String sServerFileName="";
		String sLocalFileName = "";
		java.util.Date dt = new java.util.Date(System.currentTimeMillis());
		SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		sServerFileName= "exScarch"+fmt.format(dt);
		String sWebRootPath = request.getRealPath("/");//得到你的web应用的根。
		String sPath=sWebRootPath+"exportTemp/";
		System.out.println("targetfile==============================================================》"+sPath);
		request.setAttribute("xlsName",sServerFileName+".xls");
		String execlName = sPath+sServerFileName+".xls";
		//sServerFileName =sServerFileName + sExt;
		//如果不存在该目录，则新建一个
		//File dir =new File(sPath);
		File dir =new File(sPath);
		if (!dir.exists()){
			dir.mkdirs();
		}
		String targetfile = execlName;// 输出的excel文件名  
        String worksheet = "check_"+sServerFileName;// 输出的excel文件工作表名
        
        String[] title = { "行号","申请单编号", "申请日期", "业务类型", "项目编码", "项目名称", "状态"};// excel工作表的标题
       	WritableWorkbook workbook;
       
       	 try {  
            //System.out.println("begin");  
  
            OutputStream os = new FileOutputStream(targetfile);  
            workbook = Workbook.createWorkbook(os);  
            WritableSheet sheet = workbook.createSheet(worksheet, 0); // 添加第一个工作表   
            //设置内容格式  
            WritableCellFormat contentFormat = new WritableCellFormat();  
            contentFormat.setAlignment(Alignment.CENTRE);  
            Label label;  
            for (int i = 0; i < title.length; i++) {  
                // Label(列号,行号 ,内容 )  
                label = new jxl.write.Label(i, 0, title[i]); // put the title in  
                                                                // row1  
                label.setCellFormat(contentFormat);  
                sheet.addCell(label);  
            }
            if(map!=null){
            	List list  = (List)map.get("data");
            		System.out.println("targetfile==============================================================》"+list.size());
            	if(list.size()>0){
            		for(int j=0;j<list.size();j++){
            			Object[] ex = (Object[])list.get(j);
            			//label = new jxl.write.Label(j, 0, ""+j);
            			System.out.println("targetfile==============================================================》"+ex.length);
            			for(int k=0;k<ex.length;k++){
            				if(k==0){
            					label = new jxl.write.Label(k, j+1,j+1+"");
            					label.setCellFormat(contentFormat);  
                				sheet.addCell(label);
            				}else{
            					label = new jxl.write.Label(k, j+1,ex[k].toString());
            					label.setCellFormat(contentFormat);  
                				sheet.addCell(label);
            				}
            				
            			}
            		}	
            	}
            }
            workbook.write();  
            workbook.close();  
        }catch (Exception e) {  
            e.printStackTrace();  
        }  
	return sServerFileName+".xls";
}
%>